# -*- coding:utf-8 -*-
# __auth__: JIESON
# __date__: 2021/5/6 16:45
# __desc__: time_util.py 一些常用的方法
import time
from functools import wraps

from utils.logger import get_logger

logger = get_logger(__file__)

# time type
DATE_TIME_1 = "%Y-%m-%d"
DATE_TIME_2 = DATE_TIME_1 + " %H:%M:%S"
DATE_TIME_3 = DATE_TIME_2 + " %z"
DATE_TIME_4 = "%Y%m%d%H%M%S"


def log_time(f):
    # 性能统计
    @wraps(f)
    def inner(*arg, **kwarg):
        s_time = time.time()
        res = f(*arg, **kwarg)
        logger.debug(f'{f.__name__} 耗时：{round(time.time() - s_time, 2)}秒')
        return res

    return inner


def str_time_2_timestamp(str_time, time_type):
    """
    字符串时间转时间戳
    """
    time_array = time.strptime(str_time, time_type)
    timestamp = int(time.mktime(time_array))
    return timestamp


def timestamp_2_str_time(timestamp, time_type=None):
    """
    时间戳转时间字符串
    """
    if time_type is None:
        time_type = "%Y-%m-%d %H:%M:%S %z"
    return time.strftime(time_type, time.localtime(timestamp))


def time_to_second(t):
    """
    time to second
    """
    try:
        return int(float(t))
    except:
        pass

    if "s" in t:
        t = t.replace('s', '')
        return int(t)
    elif "m" in t:
        t = t.replace('m', '')
        return int(t) * 60
    elif "h" in t:
        t = t.replace('h', '')
        return int(t) * 60 * 60
    elif "d" in t:
        t = t.replace("d", "")
        return int(t) * 24 * 60 * 60


if __name__ == "__main__":
    str_time = "2021-05-06 16:50:01 +0800"
    # print(str_time_2_timestamp(str_time, "%Y-%m-%d %H:%M:%S %z"))
    # print(time_to_second('50 m'))
    print(timestamp_2_str_time(int(time.time() * 1000)))
